<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDB" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<title>Editing Values in the Rows of a DataTable</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Editing Values in the Rows of a DataTable</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div id="outError" runat="server">&nbsp;</div>

<b>The original table contents</b>:
<asp:datagrid id="dgrResult1" runat="server" /><br />
<b>The table contents after editing Rows(0)</b>:
<asp:datagrid id="dgrResult2" runat="server" /><br />
<b>The table contents after calling CancelEdit()</b>:
<asp:datagrid id="dgrResult3" runat="server" /><br />
<b>The table contents after repeating the edit</b>:
<asp:datagrid id="dgrResult4" runat="server" /><br />
<b>The table contents after calling EndEdit()</b>:
<asp:datagrid id="dgrResult5" runat="server" /><br />

<script language="vb" runat="server">

Sub Page_Load()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect = ctlConnectStrings.OLEDBConnectionString
   outConnect.innerText = strConnect 'and display it

   'specify the SELECT statement to extract the BookList data
   Dim strSelect As String
   strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861004%'"
   outSelect.innerText = strSelect   'and display it

   'create a new DataSet object
   Dim objDataSet As New DataSet()

   Try

      'create a new Connection object using the connection string
      Dim objConnect As New OleDBConnection(strConnect)

      'create a new DataAdapter object
      Dim objDataAdapter As New OleDBDataAdapter(strSelect, objConnect)

      'get the data from the "BookList" table in the database and
      'put it into a table named "Books" in the DataSet object
      objDataAdapter.Fill(objDataSet, "Books")

   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accesing data</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   Dim objTable As DataTable
   objTable = objDataSet.Tables("Books")

   'assign the DataTable's DefaultView object to the DataGrid control
   dgrResult1.DataSource = objTable.DefaultView
   dgrResult1.DataBind()  'and bind (display) the data

   'now edit the first row
   Dim objRow As DataRow
   objRow = objTable.Rows(0)

   objRow.BeginEdit

   'change some of the values in the row
   objRow("ISBN") = "2000000000"
   objRow("Title") = "Professional Video Recorder Programming"
   objRow("PublicationDate") = "2001-03-01"

   'then display the contents again
   dgrResult2.DataSource = objTable.DefaultView
   dgrResult2.DataBind()

   'now we can check if the values are valid
   If objRow("ISBN", DataRowversion.Proposed) > "1999999999" Then
      objRow.CancelEdit
   Else
      objRow.EndEdit
   End If

   'then display the contents again
   dgrResult3.DataSource = objTable.DefaultView
   dgrResult3.DataBind()

   objRow.BeginEdit
   'change some of the values in the row
   objRow("ISBN") = "1234567800"
   objRow("Title") = "Professional Video Recorder Programming"
   objRow("PublicationDate") = "2000-10-10"

   'and display the contents again
   dgrResult4.DataSource = objTable.DefaultView
   dgrResult4.DataBind()

   'now we can check if the values are valid
   If objRow("ISBN", DataRowversion.Proposed) > "1999999999" Then
      objRow.CancelEdit
   Else
      objRow.EndEdit
   End If

   'and display the contents again
   dgrResult5.DataSource = objTable.DefaultView
   dgrResult5.DataBind()

End Sub
</script>


<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
